<?php
class Utility {

    public function __construct() {

    }

	public function init_series() { // must has para: $no_series
		$new_no = '';
		//$last_no = $this->get_last_no($no_series);
		$last_no = 'P0099'; //hard code
		//$increase_step = $this->get_increase_step($no_series);
		$increase_step = 1; //default setup
		if ($last_no = '') {
		} else {
			$new_no = $this->get_next_no($last_no, $increase_step);
		}
		return $new_no;
    }
	public function get_next_no($last_no, $increase_step) {
		//Split string: matches[1] is text_format, matches[2] is number_format
		preg_match("/(.*?)(\d+)$/", $last_no, $matches); //ex: P0099
		
		$new_suffix = $matches[2] + $increase_step; // ex: 0099 -> 99 + 1 = 100

		$prefix_strlen = strlen($last_no) - strlen($new_suffix); //ex: strlen("P0")
		$new_prefix = substr ($last_no, 0, $prefix_strlen); //ex: P0

		$new_no = $new_prefix . $new_suffix;

		return $new_no; //ex: P0100
	}
	public function get_last_no($no_series) {
		
	}
    public function get_increase_step($no_series) {
	
	}
	public function WriteLog($message) {
        
    }

    public function FolderToFolder($source, $dest) {
        if (!is_dir($dest)) {
            mkdir($dest);
        }
        if (is_dir($source)) {
            $dir_handle = opendir($source);
            while ($file = readdir($dir_handle)) {
                if ($file != "." && $file != "..") {
                    if (is_dir($source . "/" . $file)) {
                        if (!is_dir($dest . "/" . $file)) {
                            mkdir($dest . "/" . $file, 0755);
                        }
                        $this->folderToFolder($source . "/" . $file, $dest . "/" . $file);
                    } else {
                        copy($source . "/" . $file, $dest . "/" . $file);
                    }
                }
            }
            closedir($dir_handle);
        } else {
            copy($source, $dest);
        }
    }

    public function DeleteFolder($dir) {
        $this->recursiveRemoveDirectory($dir);
        //rmdir($dir);
    }

    function recursiveRemoveDirectory($directory) {
        foreach (glob("{$directory}/*") as $file) {
            if (is_dir($file)) {
                $this->recursiveRemoveDirectory($file);
            } else {
                chown($file, 666);
                unlink($file);
            }
        }
        rmdir($directory);
    }

    public function SetPermissionFolder($source) {
        chmod($source, 0755);
    }

    function replaceArray($string, $StoreCode) {
        if (is_array($string)) {
            foreach ($string as &$sValuedetail) {
                if (is_array($sValuedetail)) {
                    $sValuedetail = $this->replaceArray($sValuedetail, $StoreCode);
                } else {
                    if (strpos($sValuedetail, $StoreCode) === false && strpos($sValuedetail, 'image/data/') === true) {
                        //$sValuedetail = str_replace('image\/data\/', 'catalog\/view\/theme\/' . $StoreCode . '\/image\/data\/', $sValuedetail);
                        $sValuedetail = str_replace('image/data/', 'catalog/view/theme/' . $StoreCode . '/image/data/', $sValuedetail);
                    }
                }
            }
        } else {
            if (strpos($string, $StoreCode) === false && strpos($string, 'image/data/') === true) {
                //$string = str_replace('image\/data\/', 'catalog\/view\/theme\/' . $StoreCode . '\/image\/data\/', $string);
                $string = str_replace('image/data/', 'catalog/view/theme/' . $StoreCode . '/image/data/', $string);
            }
        }

        return $string;
    }

    function isValidEmail($email) {
        return filter_var($email, FILTER_VALIDATE_EMAIL)
                && preg_match('/@.+\./', $email);
    }
     function AddFolderToZipFile($zip, $path) {
        try {
            $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
// iterate over the directory
// add each file found to the archive
            foreach ($iterator as $key => $value) {
                $zip->addFile(realpath($key), $key) or die("ERROR: Could not add file: $key");
            }
        } catch (Exception $e) {
            //echo 'Message: ' .$e->getMessage();
            //error_reporting(0);
        }
    }

}

?>